package com.example.android.test.project2;

import com.example.android.javascript.project2.JavscriptInterface;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class DisplayHomePageActivity extends Activity {
  //ProgressDialog dialog;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Makes Progress bar Visible
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
        Window.PROGRESS_VISIBILITY_ON);    
    setContentView(R.layout.activity_display_home_page);
  
    // Setting up the webview
    WebView myWebView = (WebView) findViewById(R.id.home_page);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.addJavascriptInterface(new JavscriptInterface(this), "Android");
    
    // Displaying progress
    final Activity MyActivity = this;
    myWebView.setWebChromeClient(new WebChromeClient() {
      public void onProgressChanged(WebView view, int progress) {
        // Make the bar disappear after URL is loaded, and changes string to
        // Loading...
        MyActivity.setTitle("Loading...");
        MyActivity.setProgress(progress * 100); // Make the bar disappear after
                                                // URL is loaded
        // Return the app name after finish loading
        if (progress == 100)
          MyActivity.setTitle(R.string.title_activity_display_home_page);
      }
    });
/**/
    myWebView.setWebViewClient(new WebViewClient() {
      //Progress at the center of the screen
      ProgressDialog  dialog = ProgressDialog.show(DisplayHomePageActivity.this, "","Please wait loading...");
      
      @Override
      public void onReceivedError(WebView view, int errorCode,
          String description, String failingUrl) {
        // Handle the error
      }

      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);          
        return true;
      }
      
      @Override
      public void onPageFinished(WebView view, String url)  {
        dialog.dismiss();
      }
            
    });

    myWebView.loadUrl("http://www.treadstoneweb.com/test.php");

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_display_home_page, menu);
    return true;
  }
}
